﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace csexplorer.UserControls
{
    public partial class ListViewExplorer : UserControl
    {
        private CsExplorerManager explorerMgr;
        public CsExplorerManager ExplorerMgr
        {
            get { return explorerMgr; }
            set { explorerMgr = value; }
        }

        public ListViewExplorer()
        {
            InitializeComponent();
        }

        private void ListViewExplorer_Load(object sender, EventArgs e)
        {
            InitListView();
        }

        public void LoadDirectoryContent(DirectoryInformation directoryInformation)
        {
            InitListView();            

            // Files
            List<FileInformation> files = explorerMgr.GetFiles(directoryInformation.Path);

            foreach(FileInformation fileInfo in files)
            {
                FileInfo fi = new FileInfo(fileInfo.FullPath);
                Int64 lFileSize = fi.Length;
                DateTime dtCreateDate, dtModifyDate;

                dtCreateDate = fi.CreationTime;
                dtModifyDate = fi.LastWriteTime;

                string[] items = new string[5];
                items[0] = fileInfo.Name;
                items[1] = HelperFunctions.FormatSize(lFileSize);

                //check if file is in local current day light saving time
                if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(dtCreateDate) == false)
                {
                    //not in day light saving time adjust time
                    items[2] = HelperFunctions.FormatDate(dtCreateDate.AddHours(1));
                }
                else
                {
                    //is in day light saving time adjust time
                    items[2] = HelperFunctions.FormatDate(dtCreateDate);
                }

                //check if file is in local current day light saving time
                if (TimeZone.CurrentTimeZone.IsDaylightSavingTime(dtModifyDate) == false)
                {
                    //not in day light saving time adjust time
                    items[3] = HelperFunctions.FormatDate(dtModifyDate.AddHours(1));
                }
                else
                {
                    //not in day light saving time adjust time
                    items[3] = HelperFunctions.FormatDate(dtModifyDate);
                }
                
                items[4] = "";

                ListViewItem lvItem = new ListViewItem(items, 0);
                listviewFiles.Items.Add(lvItem);
            }
        }

        protected void InitListView()
        {
            //init ListView control
            listviewFiles.Clear();

            ColumnHeader colName = new ColumnHeader();
            colName.Text = "Name";
            colName.Width = 150;

            ColumnHeader colSize = new ColumnHeader();
            colSize.Text = "Size";
            colSize.Width = 75;

            ColumnHeader colCreatedDate = new ColumnHeader();
            colCreatedDate.Text = "Created";
            colCreatedDate.Width = 140;

            ColumnHeader colModifiedDate = new ColumnHeader();
            colModifiedDate.Text = "Modified";
            colModifiedDate.Width = 140;

            ColumnHeader colComment = new ColumnHeader();
            colComment.Text = "Comment";
            colComment.Width = 100;
            

            //create column header for ListView
            listviewFiles.Columns.Add(colName);
            listviewFiles.Columns.Add(colSize);
            listviewFiles.Columns.Add(colCreatedDate);
            listviewFiles.Columns.Add(colModifiedDate);
            listviewFiles.Columns.Add(colComment);
        }

        private void listviewFiles_Resize(object sender, EventArgs e)
        {

        }
    }
}
